
let fs = require('fs');
let path = require('path');
let glob = require('glob');
let gulp = require('gulp');

let less = require('gulp-less');
let rename = require('gulp-rename');
let uglify = require('gulp-uglify');
let minify = require('gulp-minify-css');
let concat = require('gulp-concat');
let profixer = require('gulp-autoprefixer');
let merge = require('merge-stream');
let rjs = require('requirejs');
let hash = require('flf-gulp-file-hash');

let ejs = require('flf-gulp-ejs');
let watch = require('gulp-watch');
let colors = require('colors');


let main = ['../../js/main'].concat(
    glob.sync('../../components/*.js').map(e => e.replace(/\.js$/, '')),
    glob.sync('./components/*.js').map(e => e.replace(/\.js$/, '')),
    glob.sync('./libs/*.js').map(e => e.replace(/\.js$/, '')),
    glob.sync('./pages/*.js').map(e => e.replace(/\.js$/, ''))
)


let distpath = './dist'
gulp.task('css', () => {
    return gulp.src(['../../css/*.css', 'css/*.css'])
                .pipe( minify() )
                .pipe( profixer() )
                .pipe( concat('common.min.css') )
                .pipe( gulp.dest(`${distpath}/css`) )

})

gulp.task('copy', () => {
    return merge(
        gulp.src(['../../libs/require.js'], {base: '../../'}),
        gulp.src(['../../fonts/*.*'], {base: '../../'}),
        gulp.src(['imgs/*.*', 'fonts/*.*'], {base: './'})
    )
    .pipe( gulp.dest(distpath) )
})

gulp.task('ejs', () => {
    let tplPath = './tpl';
    if(!glob.sync(tplPath).length) {
        fs.mkdirSync(tplPath);
    } else {
        return gulp.src(['tpl/**/*.ejs'])
                    .pipe(ejs({moduleName: 'tpl'}))
                    .pipe(uglify())
                    .pipe( gulp.dest('./', {overwrite: true}) )
    }
})

// console.log('-----\n', main)
gulp.task('js', ['css', 'copy', 'ejs'], () => {
    rjs.optimize({
        baseUrl: './',
        paths: {
            "jquery": "../../libs/jquery",
            "pm": "../../js/pageViewManager",
            "base": "../../components/base",
            "dialog": "../../components/dialog",
            "pageview": "../../components/pageview",
            "utils": "../../js/utils",
            "spin": "../../libs/spin",
            "spinjquery": "../../libs/spin.jquery",
            "polyfill": "../../libs/polyfill",
            photoswipemin: "../../libs/photoswipe.min",
            photoswipeuimin: "../../libs/photoswipe-ui-default.min",
            photoswipe: "../../components/photoswipe"
        },
        include: main,
        out: `${distpath}/js/main.min.js`,
        "optimize": "uglify2",
        "uglify2":{
            output:{
                "quote_keys":true
            }
        }
    }, res => {
        gulp.run('html');
    })
})

gulp.task('hash', () => {
    return gulp.src(['dist/css/*.css', 'dist/js/*.js']).pipe(hash());
})


gulp.task('html', ['hash'], () => {
    gulp.src('./index.tpl')
        .pipe( rename('index.html') )
        .pipe( hash.url(distpath) )
        .pipe( gulp.dest(distpath) );
})

gulp.task('default', ['js']);

gulp.task('dev', ['ejs'], () => {
    watch(['./tpl/*.ejs'], (obj) => {
        let filename = obj.history.shift().split('/').pop();
        switch(obj.event) {
            case 'add':
                console.log(`-----${filename} 被添加-----`.red);
                break;
            case 'change':
                console.log(`-----${filename} 被修改-----`.red);
                break;
            case 'unlink':
                console.log(`-----${filename} 被删除-----`.red);
                break;        
        }
        gulp.run('ejs');
    });
})
